package com.e2oto.seller.modules.seller.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.base.RecycleViewAdapter;
import com.e2oto.seller.common.base.RecycleViewHolder;
import com.e2oto.seller.common.utils.LogTools;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.seller.adapter.GoodsManageCategoryAdapter;
import com.e2oto.seller.modules.seller.adapter.GoodsManageContentAdapter;
import com.e2oto.seller.modules.seller.bean.GoodsCategoryBean;
import com.e2oto.seller.modules.seller.bean.RxBusBean;
import com.e2oto.seller.modules.seller.presenter.GoodsManagePresenter;
import com.e2oto.seller.modules.seller.views.GoodsManageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZhangLiQiang on 2017/11/28.
 */

public class GoodsManageActivity extends BaseMvpActivity<GoodsManageView, GoodsManagePresenter>
        implements GoodsManageView, TabLayout.OnTabSelectedListener, RecycleViewAdapter.ItemClickListener, GoodsManageContentAdapter.OnContentClick {
    @BindView(R.id.goods_manage_category_recycle_view)
    RecyclerView categoryRecyclerView;
    @BindView(R.id.goods_mamage_goods_recycle_view)
    RecyclerView contentRecyclerView;
    @BindView(R.id.goods_manage_tab_layout)
    TabLayout tabLayout;

    private GoodsManageCategoryAdapter categoryAdapter;
    private GoodsManageContentAdapter contentAdapter;

    @Override
    protected GoodsManagePresenter createPresenter() {
        return new GoodsManagePresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_goods_manage;
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
        tabLayout.addTab(tabLayout.newTab().setText("零售"));
        tabLayout.addTab(tabLayout.newTab().setText("批发"));
        tabLayout.addOnTabSelectedListener(this);

        categoryAdapter = new GoodsManageCategoryAdapter(new ArrayList<GoodsCategoryBean>());
        contentAdapter = new GoodsManageContentAdapter(new ArrayList<GoodsCategoryBean.GoodsBean>(), this);
        categoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        categoryRecyclerView.setAdapter(categoryAdapter);
        contentRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        contentRecyclerView.setAdapter(contentAdapter);
        categoryAdapter.setOnItemClickListener(R.id.category, this);
        contentAdapter.setOnContentClick(this);

    }

    @OnClick({R.id.goods_manage_back, R.id.goods_manage_add_goods, R.id.goods_manage_handle_all})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.goods_manage_back:
                finish();
                break;
            case R.id.goods_manage_add_goods:
                Intent intent = new Intent(this, AddGoodsBaseInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.goods_manage_handle_all:
                contentAdapter.resetVisibility();
                break;
        }
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @Override
    protected void onReceiveRxMessage(RxBusBean anyType) {
        super.onReceiveRxMessage(anyType);
        if (anyType.getType() == 4457) {//通知刷新页面 最好不请求数据
            if (tabLayout.getSelectedTabPosition() == 0) {

            } else {

            }
        }
    }

    @Override
    public void getCategoryListCallback(List<GoodsCategoryBean> list) {
        categoryAdapter.getDataList().clear();
        categoryAdapter.getDataList().addAll(list);
        categoryAdapter.notifyDataSetChanged();

        if (categoryAdapter.getDataList() != null && categoryAdapter.getDataList().size() > 0) {
            contentAdapter.getDataList().clear();
            contentAdapter.getDataList().addAll(categoryAdapter.getDataList().get(0).getGoods());
            contentAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (tab.getPosition() == 0) {
            //拿零售商品列表
            mPresenter.getGoodsList();

            categoryAdapter.getDataList().clear();
            categoryAdapter.notifyDataSetChanged();
            contentAdapter.getDataList().clear();
            contentAdapter.notifyDataSetChanged();
        } else {
            //拿批发商品列表
            mPresenter.getBatchGoodsList();
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }

    @Override
    public void onItemClicked(View view, int position) {
        switch (view.getId()) {
            case R.id.category:
                contentAdapter.getDataList().clear();
                List<GoodsCategoryBean.GoodsBean> list = categoryAdapter.getDataList().get(position).getGoods();
                contentAdapter.getDataList().addAll(list);
                contentAdapter.notifyDataSetChanged();
                contentAdapter.resetSelectedStatus();//重置和隐藏选中状态
                break;
        }
    }

    @Override
    public void onClickDelete(RecycleViewHolder holder, GoodsCategoryBean.GoodsBean goodsBean, int position) {
        contentAdapter.getDataList().remove(holder.getAdapterPosition());
        contentAdapter.notifyItemRemoved(holder.getAdapterPosition());
    }

    @Override
    public void onClickSelected(RecycleViewHolder holder, GoodsCategoryBean.GoodsBean goodsBean, int position) {
        boolean oldSelectedStatus = contentAdapter.getDataList().get(position).isSelected();
        contentAdapter.getDataList().get(position).setSelected(!oldSelectedStatus);
        contentAdapter.notifyItemChanged(holder.getAdapterPosition());
    }

    @Override
    public void onClickRoot(RecycleViewHolder holder, GoodsCategoryBean.GoodsBean goodsBean, int position) {
        Intent intent = new Intent(this, GoodsPreviewActivity.class);
        intent.putExtra("goods_id", contentAdapter.getDataList().get(holder.getAdapterPosition()).getGoods_id() + "");
        startActivity(intent);
    }
}
